package com.example.administrator.yiroote.activity;

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.example.administrator.yiroote.R;
import com.example.administrator.yiroote.fragment.StoreFragment;
import com.example.administrator.yiroote.fragment.PersonalFragment;
import com.example.administrator.yiroote.fragment.MyHomFragment;

public class HomeActivity extends AppCompatActivity implements View.OnClickListener{
   /* private ViewPager mViewPager;
    private FragmentPagerAdapter mFragmentAdapter;
    //装载fragment的集合
   private List<Fragment> mFragments;*/

    private LinearLayout mTabStore;
    private LinearLayout mTabPublish;
    private LinearLayout mTabMyHome;
    private LinearLayout mTabPersonal;

    private ImageButton mImgStore;
    private ImageButton mImgPublish;
    private ImageButton mImgMyHome;
    private ImageButton mImgPersonal;

    private Fragment mFmStore;
    private Fragment mFmPublish;
    private Fragment mFmMyHome;
    private Fragment mFmPersonal;

    private TextView mTextColorStore;
    private TextView mTextColorPublish;
    private TextView mTextColorMyHome;
    private TextView mTextColorPersonal;

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_home );

        initViews();
        initEvents();
        setselect(0);
    }
    private void initViews(){
        mTabStore=(LinearLayout)findViewById(R.id.id_tab_store);
        mTabPublish=(LinearLayout)findViewById(R.id.id_tab_publish);
        mTabMyHome=(LinearLayout)findViewById(R.id.id_tab_myhome);
        mTabPersonal=(LinearLayout)findViewById(R.id.id_tab_personal);

        mImgStore=(ImageButton)findViewById(R.id.id_tab_store_img);
        mImgPublish=(ImageButton)findViewById(R.id.id_tab_publish_img);
        mImgMyHome=(ImageButton)findViewById(R.id.id_tab_myhome_img);
        mImgPersonal=(ImageButton)findViewById(R.id.id_tab_personal_img);

        mTextColorStore=(TextView)findViewById(R.id.id_tvColor_store) ;
        mTextColorPublish=(TextView)findViewById(R.id.id_tvColor_publish);
        mTextColorMyHome=(TextView)findViewById(R.id.id_tvColor_myhome);
        mTextColorPersonal=(TextView)findViewById(R.id.id_tvColor_personal);

        //mViewPager=(ViewPager)findViewById(R.id.iv_viewpager);

    }
    private void resetImgs() {
        mImgStore.setImageResource(R.drawable.tab_home_normal);
        mImgPublish.setImageResource(R.drawable.tab_collect_normal);
        mImgMyHome.setImageResource(R.drawable.tab_search_normal);
        mImgPersonal.setImageResource(R.drawable.tab_collect_normal);

    }
    private  void resetTextColor(){
        mTextColorStore.setTextColor(mTextColorStore.getResources().getColor(R.color.white));
        mTextColorPublish.setTextColor(mTextColorPublish.getResources().getColor(R.color.white));
        mTextColorMyHome.setTextColor(mTextColorMyHome.getResources().getColor(R.color.white));
        mTextColorPersonal.setTextColor(mTextColorPersonal.getResources().getColor(R.color.white));
    }
    public void initEvents(){
        mTabStore.setOnClickListener(this);
        mTabPublish.setOnClickListener(this);
        mTabMyHome.setOnClickListener(this);
        mTabPersonal.setOnClickListener(this);

    }

    @Override
    public void onClick(View v) {
          resetImgs();
        resetTextColor();
        switch (v.getId())
        {
            case R.id.id_tab_store:
              setselect(0);
                break;
            case R.id.id_tab_publish:
                setselect(1);
                break;
            case R.id.id_tab_myhome:
                setselect(2);
                break;
            case R.id.id_tab_personal:
                setselect(3);
                break;
            default:
                break;

        }

    }

    private void setselect(int i) {
        FragmentManager fm=getSupportFragmentManager();
        FragmentTransaction ft=fm.beginTransaction();
          hideFragment(ft);
        switch (i)
        {
            case 0:
                if (mFmStore == null)
                {
                    mFmStore = new StoreFragment();
                    ft.add(R.id.id_content, mFmStore);
                } else
                {
                    ft.show(mFmStore);
                }
                mImgStore.setImageResource(R.drawable.tab_home_pressed);
                mTextColorStore.setTextColor(mTextColorStore.getResources().getColor(R.color.green));

                break;
            case 1:
                if (mFmPublish == null)
                {
                    mFmPublish = new MyHomFragment();
                    ft.add(R.id.id_content, mFmPublish);
                } else
                {
                    ft.show(mFmPublish);

                }
                mImgPublish.setImageResource(R.drawable.tab_collect_pressed);
                mTextColorPublish.setTextColor(mTextColorPublish.getResources().getColor(R.color.green));
                break;
                       case 2:
                if (mFmMyHome== null)
                {
                    mFmMyHome = new PersonalFragment();
                    ft.add(R.id.id_content, mFmMyHome);
                } else
                {
                    ft.show(mFmMyHome);
                }
                mImgMyHome.setImageResource(R.drawable.tab_search_pressed);
                mTextColorMyHome.setTextColor(mTextColorMyHome.getResources().getColor(R.color.green));
                break;
            case 3:
                if (mFmPersonal== null)
                {
                    mFmPersonal = new PersonalFragment();
                    ft.add(R.id.id_content, mFmPersonal);
                } else
                {
                    ft.show(mFmPersonal);
                }
                mImgPersonal.setImageResource(R.drawable.tab_search_pressed);
                mTextColorPersonal.setTextColor(mTextColorPersonal.getResources().getColor(R.color.green));
                break;



            default:
                break;
        }
       // mViewPager.setCurrentItem(i);
        ft.commit();
    }

    private void hideFragment(FragmentTransaction ft) {
        if (mFmStore!=null)
        {
            ft.hide(mFmStore);
        }
        if (mFmPublish!=null)
        {
            ft.hide(mFmPublish);
        }
        if (mFmMyHome!=null)
        {
            ft.hide(mFmMyHome);
        }
        if (mFmPersonal!=null)
        {
            ft.hide(mFmPersonal);
        }
    }
}
